<template>
  <el-dialog
    draggable
    overflow
    :title="titleMap[mode]"
    v-model="visible"
    :width="1024"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <!-- <sc-water-mark ref="wm" text="已审核" subtext="誉卓纱线"> -->
    <sk-tabs type="border-card">
      <el-tab-pane ref="tab" label="基础信息">
        <el-divider content-position="left">基本信息</el-divider>
        <el-form
          :model="form"
          :rules="rules"
          ref="dialogForm"
          label-width="70px"
          label-position="right"
        >
          <el-row align="bottom">
            <el-col :span="7" align="bottom">
              <el-form-item label="单据日期" prop="billdate">
                <el-date-picker
                  v-model="form.billdate"
                  type="date"
                  placeholder="选择日期"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                />
                <!-- <el-input label="单据日期" v-model="form.code" clearable></el-input> -->
              </el-form-item>
            </el-col>
            <el-col :span="7" align="bottom">
              <el-form-item label="编号" prop="code">
                <el-input label="编号" v-model="form.code" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-divider content-position="left">库存信息</el-divider>
        <sc-VxeTable
          ref="save"
          :gridOps="gridOps"
          :gridEvts="gridEvts"
          :data="form.details"
          :tablecode="'storage_action_other_save'"
        >
        </sc-VxeTable>
      </el-tab-pane>
      <el-tab-pane label="附件">
        <sk-upload
          v-model="form.attachments"
          :limit="10"
          draggable
          tip="单个文件不要超过10M,请上传图像格式文件"
        >
        </sk-upload>

        <el-dialog v-model="dialogVisible">
          <img w-full :src="dialogImageUrl" alt="Preview Image" />
        </el-dialog>
      </el-tab-pane>
    </sk-tabs>
    <!-- </sc-water-mark> -->
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import XEUtils from "xe-utils";
export default {
  emits: ["success", "closed"],
  components: {},
  data() {
    return {
      mode: "add",
      dialogImageUrl: "",
      dialogVisible: false,
      openChooseDlg: false,
      gridOps: {
        columns: [
          {
            field: "auxiliary_quantity",
            event: {
              keyup: ({ row }) => {
                row["quantity"] = row["auxiliary_quantity"] * 25;
                row["amount"] = XEUtils.toFixed(row["quantity"] * row["price"], 2);
              },
            },
          },
          {
            field: "material.fullname",
            event: {
              select: ({ e, row }) => {
                row["material"]["spec"] = e.spec;
                row["unit"] = e.uunit;
                row["unit_id"] = e.uunit_id;
                console.log(e);
              },
            },
          },
          {
            field: "color.name",
            event: {
              select: ({ e, row }) => {
                row["color"]["code"] = e.code;
              },
            },
          },
          {
            field: "price",
            event: {
              keyup: ({ row }) => {
                row["amount"] = XEUtils.toFixed(row["quantity"] * row["price"], 2);
              },
            },
          },
          {
            field: "amount",
            event: {
              keyup: ({ row }) => {
                row["price"] = XEUtils.toFixed(row["amount"] / row["quantity"], 2);
              },
            },
          },
          {
            field: "quantity",
            event: {
              keyup: ({ row }) => {
                row["amount"] = XEUtils.toFixed(row["quantity"] * row["price"], 2);
              },
            },
          },
        ],
        height: 300,
      },
      gridEvts: {
        choose: () => (this.openChooseDlg = true),
        copy: ({ button }) => {
          this.$TOOL.objCopy(button.selectRecords).map(v => {
            delete v.id;
            this.form.details.push(v);
          });
        },
      },
      titleMap: {
        add: "新增",
        edit: "编辑",
      },

      form: {
        id: "",
        billdate: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
        code: "",
        remark: "",
        details: [],
        attachments: [],
      },
      rules: { billdate: { required: true, message: "请选择单据日期" } },
      visible: false,
      isSaveing: false,
    };
  },
  mounted() {},
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async valid => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.storage.other.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });
    },
    async getItems(id) {
      var res = await this.$API.storage.other.info.get({
        filter: [{ id: id }],
      });
      if (res.code == 200) {
        this.form = res.data;
      }
    },

    //表单注入数据
    setData(data) {
      this.getItems(data.id);
    },
  },
};
</script>

<style></style>
